package com.tim.app.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.application.library.runtime.event.EventManager;
import com.application.library.widget.ScrollGridView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.HorizontalDividerItemDecoration;
import com.application.library.widget.recycle.VerticalDividerItemDecoration;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.EventTag;
import com.tim.app.server.entry.Advertisement;
import com.tim.app.server.entry.MusicAlbum;
import com.tim.app.server.entry.Special;
import com.tim.app.server.entry.Story;
import com.tim.app.ui.activity.found.AllMusicAlbumActivity;
import com.tim.app.ui.activity.found.EveryDayHearDetailActivity;
import com.tim.app.ui.activity.found.HotSpecialActivity;
import com.tim.app.ui.activity.found.MusicAlbumDetailActivity;
import com.tim.app.ui.activity.found.SpecialDetailActivity;
import com.tim.app.ui.adapter.found.HotEverydayHearAdapter;
import com.tim.app.ui.adapter.found.HotMusciAlbumAdapter;
import com.tim.app.ui.adapter.found.SpecialSubjcetAdapter;
import com.tim.app.util.ViewGT;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现的header
 */

public class FoundHeadView extends LinearLayout implements View.OnClickListener {

    public static final String TAG = "SelectedHeadView";

    private BannerView bannerView;

    private RelativeLayout rlMoreSpecial;
    private WrapRecyclerView wrvSpecial;
    private SpecialSubjcetAdapter specialSubjcetAdapter;
    private List<Special> specialList;

    private RelativeLayout rlMoreEverydayHear;
    private WrapRecyclerView wrvEverydayHear;
    private HotEverydayHearAdapter everydayHearAdapter;
    private List<Story> fullStoryList;
    private List<Story> storyList;
    private ImageView ivContinuousPlay;

    private RelativeLayout rlMoreMusicAlbum;
    private ScrollGridView sgvMusicAlbum;
    private HotMusciAlbumAdapter musicAlbumAdapter;
    private List<MusicAlbum> musicAlbumList;

    public FoundHeadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        bannerView = (BannerView) findViewById(R.id.bvAdvertisement);
        rlMoreSpecial = (RelativeLayout) findViewById(R.id.rlMoreSpecial);
        wrvSpecial = (WrapRecyclerView) findViewById(R.id.wrvSpecial);
        specialList = new ArrayList<>();
        specialSubjcetAdapter = new SpecialSubjcetAdapter(getContext(), specialList);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        View headerView = new View(getContext());
        headerView.setLayoutParams(new RecyclerView.LayoutParams((int) RT.getDensity() * 9, RecyclerView.LayoutParams.MATCH_PARENT));
        headerView.setBackgroundColor(getContext().getResources().getColor(R.color.transparent));
        wrvSpecial.addHeaderView(headerView);
        wrvSpecial.addFootView(new View(getContext()));
        wrvSpecial.addItemDecoration(new VerticalDividerItemDecoration.Builder(getContext()).color(getContext().getResources().getColor(R.color.transparent)).size((int) getContext().getResources().getDimension(R.dimen.splitLineWidth)).build());
        wrvSpecial.setLayoutManager(layoutManager);
        rlMoreSpecial.setOnClickListener(this);
        wrvSpecial.setAdapter(specialSubjcetAdapter);

        specialSubjcetAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {
                SpecialDetailActivity.start(getContext(), specialList.get(position), false);
            }
        });


        rlMoreEverydayHear = (RelativeLayout) findViewById(R.id.rlMoreEverydayHear);
        wrvEverydayHear = (WrapRecyclerView) findViewById(R.id.wrvEverydayHear);
        ivContinuousPlay = (ImageView) findViewById(R.id.ivContinuousPlay);
        storyList = new ArrayList<>();
        fullStoryList = new ArrayList<>();
        everydayHearAdapter = new HotEverydayHearAdapter(getContext(), storyList);
        rlMoreEverydayHear.setOnClickListener(this);
        ivContinuousPlay.setOnClickListener(this);

        LinearLayoutManager everydayHearlayoutManager = new LinearLayoutManager(getContext());
        everydayHearlayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrvEverydayHear.setLayoutManager(everydayHearlayoutManager);
        wrvEverydayHear.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext()).color(getContext().getResources().getColor(R.color.transparent)).size((int) getContext().getResources().getDimension(R.dimen.dimen_10)).build());
        wrvEverydayHear.setAdapter(everydayHearAdapter);

        everydayHearAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {
                ViewGT.playStory(getContext(), storyList, position);
            }
        });

        rlMoreMusicAlbum = (RelativeLayout) findViewById(R.id.rlMoreMusicAlbum);
        sgvMusicAlbum = (ScrollGridView) findViewById(R.id.sgvMusicAlbum);
        musicAlbumList = new ArrayList<>();
        musicAlbumAdapter = new HotMusciAlbumAdapter(getContext());
        musicAlbumAdapter.setData(musicAlbumList);
        sgvMusicAlbum.setAdapter(musicAlbumAdapter);
        rlMoreMusicAlbum.setOnClickListener(this);
        sgvMusicAlbum.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MusicAlbumDetailActivity.start(getContext(), musicAlbumList.get(position), false);
            }
        });
    }

    public View getBannerView() {
        return bannerView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlMoreSpecial://更多专题
                HotSpecialActivity.start(getContext());
                break;
            case R.id.rlMoreEverydayHear://更多每日一听
                EveryDayHearDetailActivity.start(getContext(), false);
                break;
            case R.id.rlMoreMusicAlbum://更多音乐专辑
                AllMusicAlbumActivity.start(getContext());
                break;
            case R.id.ivContinuousPlay://连续播放
                if (isPlay) {
                    EventManager.ins().sendEvent(EventTag.GO_PLAY_OR_PAUSE, 0, 0, null);
                } else {
                    if (currentPlayIndex != -1 && currentPlayIndex < 4) {
                        EventManager.ins().sendEvent(EventTag.GO_PLAY_OR_PAUSE, 0, 0, null);
                    } else {
                        ViewGT.playStory(getContext(), fullStoryList, 0);
                    }
                }
                break;
        }
    }

    public void setData(List<Advertisement> banners) {
        bannerView.setData(banners);
    }

    boolean isPlay;

    int currentPlayIndex = -1;

    public void setPlayData(int index, List<Story> dataList, int stated) {
        if (null == dataList || index > dataList.size() - 1) {
            return;
        }
        Story story = dataList.get(index);
        boolean isContainer = false;
        for (int i = 0; i < fullStoryList.size(); i++) {
            if (fullStoryList.get(i).getId().equals(story.getId())) {
                if (stated == 1) {
                    ivContinuousPlay.setSelected(true);
                    isPlay = true;
                    everydayHearAdapter.setIndex(i);
                    everydayHearAdapter.notifyDataSetChanged();
                    isContainer = true;
                    currentPlayIndex = i;
                }
                break;
            }
        }
        if (!isContainer) {
            isPlay = false;
            currentPlayIndex = -1;
            ivContinuousPlay.setSelected(false);
            everydayHearAdapter.setIndex(-1);
            everydayHearAdapter.notifyDataSetChanged();
        }
    }

    public void updatePlayingStated(boolean isPlaying) {
        isPlay = isPlaying;
        ivContinuousPlay.setSelected(false);
    }

    public boolean isNull() {
        if (storyList.size() == 0 || specialList.size() == 0 || fullStoryList.size() == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 更新专题
     *
     * @param specialData
     */
    public void setSpecialData(List<Special> specialData) {
        if (null != specialData) {
            specialList.clear();
            specialList.addAll(specialData);
            specialSubjcetAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 更新每日一听的数据
     *
     * @param everyDayHearData
     */
    public void setEverydayStory(List<Story> everyDayHearData) {
        if (null != everyDayHearData && everyDayHearData.size() > 0) {
            storyList.clear();
            fullStoryList.clear();
            fullStoryList.addAll(everyDayHearData);
            if (everyDayHearData.size() > 4) {
                storyList.addAll(everyDayHearData.subList(0, 4));
            } else {
                storyList.addAll(everyDayHearData);
            }
            everydayHearAdapter.notifyDataSetChanged();
            ivContinuousPlay.setVisibility(View.VISIBLE);
            rlMoreEverydayHear.setVisibility(View.VISIBLE);
        } else {
            ivContinuousPlay.setVisibility(View.GONE);
            rlMoreEverydayHear.setVisibility(View.GONE);
        }
    }

    /**
     * 更新音乐专辑
     *
     * @param musicAlbumData
     */
    public void setMusicAlbumData(List<MusicAlbum> musicAlbumData) {
        if (null != musicAlbumData) {
            musicAlbumList.clear();
            musicAlbumList.addAll(musicAlbumData);
            musicAlbumAdapter.notifyDataSetChanged();
        }
    }
}
